<?php
class Base extends BaseModel{
  var $required=array(
                        "wbm_title"=>array("type"=>"string","required"=>"typenormal","msg"=>"Title is required"),
                        "wbm_desc"=>array("type"=>"string","required"=>"typenormal","msg"=>"Description is required"),
                        "wbm_file"=>array("type"=>"fileAttachment","required"=>"typenormal","msg"=>"Picture is required")
                
                        
    );  
    var $allowed_file_types=array('jpg','gif','jpeg','png');
                
                        
    
    
    
    
function checkNewWBMDetails($array){
        $failed=false;

        foreach($this->required as $key=>$value){
            if ($value['required'] === false && strlen(trim($array[$key]))==0) continue;

            switch($value['type']){
                case 'number':
                    if(!$this->checkNumber($array[$key])){
                        $this->setError($key,$value['msg']);
                        $failed=true;
                        $break;
                    }
                    break;
                case 'string':
                    if(!$this->checkString($array[$key])){
                        $this->setError($key,$value['msg']);
                        $failed=true;
                        $break;
                    }
                    break;
                case 'email':
                    if(!$this->checkEmail($array[$key])){
                        $this->setError($key,$value['msg']);
                        $failed=true;
                        $break;
                    }
                    break;
               case 'fileAttachment':
                if(!$this->checkFileAttachment($key)){                     
                    $this->setError($key,$value['msg'].".");
                    $failed=true;
                    $break;
                }
                break;    
                                
                case 'url':
                    if($array['online']==1){
                        if(!$this->checkString($array[$key])){
                            $this->setError($key,$value['msg']);
                            $failed=true;
                            $break;
                        }
                    }
                    break;
            }


        }
        return $failed;
    }

function checkFileAttachment($var){
    $success=false;
    if(!empty($_FILES[$var]['name']) && !empty($_FILES[$var]['tmp_name'])){        
       if ($this->checkFileAllowed($_FILES[$var]['name'],$this->allowed_file_types)>0){
          
        $success=true;    
       }
    }
    
    
    return $success;
}


function checkNumber($var){
    $success=false;
    $var = trim($var);
    if(is_numeric($var)){
        $success=true;
    }
    return $success;
}

function checkFileAllowed($thefile,$allowed_types) {
$retval = -1;
      $file_title = $thefile;
    //Get file extension
    $ext_arr = split("\.",basename($file_title));
    $ext = strtolower($ext_arr[count($ext_arr)-1]); //Get the last extension

        
    if($allowed_types) {
        if(in_array($ext,$allowed_types)){
            
            $retval = 1;
        }else {
            $result = "'".$thefile."' is not a valid file.<br/> it has the wrong extension '.".$ext."',<br/> allowed extensions:'".implode(', ', $allowed_types)."'"; //Show error if any.
            $this->setError('wbm_file_invalid_file',$result);

        }
    }
//echo $ext." >> ".$retval;

 return $retval;  
}




function checkString($var){
    $success=false;
    $var = trim($var);
    if(is_string($var)){
        if(strlen($var)>0)
            $success=true;
    }
    return $success;
}

function myUpload($thefile, $uploaded_file_name) {
$operationSuccess =false;    
    if(!$thefile) return array('File_error','No file specified');    
   

    if (isset($_FILES[$thefile]) && (!empty($_FILES[$thefile]['tmp_name'])))  // file was send from browser
    {
        if ($_FILES[$thefile]['error'] == UPLOAD_ERR_OK)  // no error
        {
         //echo $_FILES[$thefile]['tmp_name'] .">> $uploaded_file_name";
            //$filename = time() . $filename;
            if (@move_uploaded_file( $_FILES[$thefile]['tmp_name'],  $uploaded_file_name )){
                
            @chmod(EDF_FILE_UPLOAD_DIR . $uploaded_file_name, 0644);
            $operationSuccess = true;
            }else{
           return array('File_error','file was not saved');            
            }   
        }
    }else{
        return array('File_error','file was not sent by browser, please try again');    
    }
    
  return $operationSuccess;  
}



 

function getReports($id=0){
   $sqlStr = "SELECT id, title, report_img, description,report_url FROM reports where deleted=0 " ;

   if (intval($id)>0){  
       $sqlStr.= " AND id = ".$id. "";
   }
      //    echo $sqlStr;           
            $reports = Database::runSelectQuery($sqlStr);
            
            if (sizeof($reports)>0){ 
                    return $reports;
            }
            
            
} 

function getScenarios($id=0){
   $sqlStr = "SELECT id, title, description, reports FROM scenarios where deleted=0 " ;

   if (intval($id)>0){  
       $sqlStr.= " AND id = ".$id. "";
   }
         // echo $sqlStr;           
            $scenarios = Database::runSelectQuery($sqlStr);
            
            if (sizeof($scenarios)>0){ 
                    return $scenarios;
            }
            
            
} 

function getBusinesses($id=0){
   $sqlStr = "SELECT id, title, description, scenarios FROM businesses where deleted=0 " ;

   if (intval($id)>0){  
       $sqlStr.= " AND id = ".$id. "";
   }
         // echo $sqlStr;           
            $dateArr = Database::runSelectQuery($sqlStr);
            
            if (sizeof($dateArr)>0){ 
                    return $dateArr;
            }
            
            
} 


function getCommentsForReport($id){
   $sqlStr = "SELECT cj.id as cId ,
    cj.title as cTitle,
     comment,
      report_id,
       rep.title,
        rep.report_img,      
        rep.report_url      
FROM reports rep
JOIN comments cj ON rep.id = cj.report_id
                                                
WHERE rep.is_activated = 1 AND cj.is_activated = 1 AND cj.deleted = 0 ";
   
   if (intval($id)>0){  
       $sqlStr.= "AND  rep.id = ".$id. "";
   }
           //  echo $sqlStr;
$comments = Database::runSelectQuery($sqlStr);

if (sizeof($comments)>0){ 
        return $comments;
}
            
            
} 

function saveBusiness($busId=0,$busTitle, $busDesc, $busScenarios, $type='edit'){

 if ( !empty($busTitle) && $type=='save' ){
     
     if ($busId>0){
         
         
            $strSql = "UPDATE businesses  SET title='$busTitle', description='$busDesc', scenarios='$busScenarios', updated_at=".time()."
   WHERE id=$busId"; 
   //echo     $strSql;
   
     }else{
   $strSql = "insert INTO businesses ( title, description, scenarios, created_at) values('$busTitle','$busDesc','$busScenarios','".time()."')";
     
     }           
   echo $strSql;   
            
   $success = Database::runQuery($strSql);
      
   if (empty($success)){
     if ($busId>0){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showBusiness.php?busId='.$busId.'&busAct=editBus');
     }else{
       header('Location: '.APPURIPATH.'showBusiness.php');
     }
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
}


function saveScenario($scnId=0,$scnTitle, $scnDesc, $scnReports, $type='edit'){

 if ( !empty($scnTitle) && $type=='save' ){
     
     if ($scnId>0){
         
         
            $strSql = "UPDATE scenarios  SET title='$scnTitle', description='$scnDesc', reports='$scnReports', updated_at=".time()."
   WHERE id=$scnId"; 
   //echo     $strSql;
   
     }else{
   $strSql = "insert INTO scenarios ( title, description, reports, created_at) values('$scnTitle','$scnDesc','$scnReports','".time()."')";
     
     }           
   echo $strSql;   
            
   $success = Database::runQuery($strSql);
      
   if (empty($success)){
     if ($scnId>0){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showScenario.php?scnId='.$scnId.'&scnAct=editScn');
     }else{
       header('Location: '.APPURIPATH.'showScenario.php');
     }
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
}



function saveReport($repId=0,$repTitle, $repDesc, $report_img,$report_url, $type='edit'){
      /*
   if (intval($cId)>0 && !empty($cTitle) && $type != 'new'  ){
   $strSql = "update comments set title='".trim($cTitle)."', updated_at='".time()."', comment='".trim($cComment)."' WHERE id=".$cId;
   //$strSql = "update comments set title='".trim($cTitle)."', updated_at='".time()."', comment='".trim($cComment)."' WHERE id=".$cId;
       
   //    echo $strSql;
            
   $success = Database::runUpdateQuery($strSql);
   
   if (empty($success)){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showReport.php?repId='.$cRepId);
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
}   */
//$report_url = clean_for_mysql($report_url);
$report_url = rawurlencode($report_url);


 if ( !empty($repTitle) && $type=='save' ){
     
     if ($repId>0){
         
         
            $strSql = "UPDATE reports  SET title='$repTitle', description='$repDesc', report_img='$report_img',report_url='$report_url', updated_at=".time()."
   WHERE id=$repId"; 
   //echo     $strSql;
   
     }else{
   $strSql = "insert INTO reports ( title, description, report_img,report_url, created_at) values('$repTitle','$repDesc','$report_img','$report_url','".time()."')";
     
     }           
      
            
   $success = Database::runQuery($strSql);
      
   if (empty($success)){
     if ($repId>0){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showReport.php?repId='.$repId.'&repAct=edit');
     }else{
       header('Location: '.APPURIPATH.'index.php');  
     }
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
}




function saveComment($cId=0,$cTitle, $cRepId, $cComment, $type='edit'){
   if (intval($cId)>0 && !empty($cTitle) && $type != 'new'  ){
   $strSql = "update comments set title='".trim($cTitle)."', updated_at='".time()."', comment='".trim($cComment)."' WHERE id=".$cId;
   //$strSql = "update comments set title='".trim($cTitle)."', updated_at='".time()."', comment='".trim($cComment)."' WHERE id=".$cId;
       
   //    echo $strSql;
            
   $success = Database::runUpdateQuery($strSql);
   
   if (empty($success)){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showReport.php?repId='.$cRepId.'&repAct=edit');
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
 if (intval($cRepId)>0 && !empty($cTitle) && $type=='new' ){
     
   $strSql = "insert INTO comments (report_id, title, comment, created_at) values('$cRepId','$cTitle','".mysql_real_escape_string($cComment)."','".time()."')";
   //$strSql = "update comments set title='".trim($cTitle)."', updated_at='".time()."', comment='".trim($cComment)."' WHERE id=".$cId;
     //echo $strSql;            
   // exit;
            
   $success = Database::runInsertQuery($strSql);
  // echo $success ;
   
    // exit;    
   if ($success>0){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showReport.php?repId='.$cRepId.'&repAct=edit');
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
}

function deleteComment($cId=0,$cRepId){
 
   if (intval($cId)>0){
       
   $strSql = "update comments set deleted=1 WHERE id=".$cId;
  //  echo   $strSql;
          
   $success = Database::runUpdateQuery($strSql);
   
   if (empty($success)){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showReport.php?repId='.$cRepId);
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
  
}

function deleteReport($repId){
 
   if (intval($repId)>0){
       
   $strSql = "update reports set deleted=1 WHERE id=".$repId;
    echo   $strSql;
          
   $success = Database::runUpdateQuery($strSql);
   
   if (empty($success)){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showScenario.php');
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
  
}
function deleteScenario($scnId){
 
   if (intval($scnId)>0){
       
   $strSql = "update scenarios set deleted=1 WHERE id=".$scnId;
    echo   $strSql;
          
   $success = Database::runUpdateQuery($strSql);
   
   if (empty($success)){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'index.php');
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
  
}

function deleteBusiness($busId){
 
   if (intval($busId)>0){
       
   $strSql = "update Businesses set deleted=1 WHERE id=".$busId;
    echo   $strSql;
          
   $success = Database::runUpdateQuery($strSql);
   
   if (empty($success)){
      // echo "comment ".$cId. " was updated";
       header('Location: '.APPURIPATH.'showBusiness.php');
       
   }else{
    echo ERROR_WRONG_ID_NO_DATA;   
   }                                        
                                
            
            
} 
  
}

function getComment($cId=0){
   $sqlStr = "SELECT cj.id ,
    cj.title as cTitle,
     comment,
      report_id
       
FROM  comments cj 
                                                
WHERE cj.is_activated = 1 AND cj.deleted = 0 AND  cj.id = ".$cId. "";
   
            
$comments = Database::runSelectQuery($sqlStr);

if (sizeof($comments)>0){ 
        return $comments;
}  else{
    echo ERROR_WRONG_ID_NO_DATA;
}
            
            
} 



function showReportsLinks(){
   
            $reports = $this->getReports();
$html="";            
            if (sizeof($reports)>0){ 
           $ct=1;     
            foreach ($reports as $report){ 
                 $id=$title=$thumb = "";  
                foreach ($report as $key=>$val){ 
                    
                    
                    if ($key==0){ $id = $val;}
                    if ($key==1){ $title = $val;}
                    if ($key==2){ $thumb = $val;}
                   // sprintf('<a href="?report=%s" title="%s"> %s</a> <br />', $report[0],  $report[1],  $report[1]);
                   
                }
               $html .= $ct .':<span class="links"> <a href="showReport.php?repId='.$id.'" title="view Step: '.$title.'">'.$title.'</a> ';
               $html .= ' ::  <span class="actionlinks">';
               $html .= ' <a href="showReport.php?repId='.$id.'" title="view Step: '.$title.'">Preview</a> ';
               $html .= ' |  <a href="showReport.php?repId='.$id.'&repAct=edit" title="edit Step: '.$title.'">Edit</a> ';
             //  $html .= ' |  <a href="submitReport.php?repId='.$id.'" title="Submit report: '.$title.' for review">Submit for review</a> ';
               //$html .= ' | <a href="deleteReport.php?repId='.$id.'" title="delete report: '.$title.'">Delete</a>';
               $html .= '</span></span><br/><br/>';
            $ct++;
            }
            
            echo $html;
            }
}

function showBusinessLinks(){
   
            $businessArr = $this->getBusinesses();
$html="";            
            if (sizeof($businessArr)>0){ 
           $ct=1;     
           
           //var_dump($scenarios);
           
            foreach ($businessArr as $business){ 
                 $id=$title=$thumb = "";  
                 
                    
                    
                    $id = $business[0];
                    $title = $business[1];
                    $desc = $business[2];
                    
                   // sprintf('<a href="?Scenario=%s" title="%s"> %s</a> <br />', $business[0],  $business[1],  $business[1]);
                   
                
               $html .= $ct .':<span class="links"> <a href="showBusiness.php?busId='.$id.'" title="view Business: '.$title.'">'.$title.'</a> ';
               $html .= ' ::  <span class="actionlinks">';
               $html .= ' <a href="showBusiness.php?busId='.$id.'" title="view Business: '.$title.'">Preview</a> ';
               $html .= ' |  <a href="showBusiness.php?busId='.$id.'&busAct=editBus" title="edit Business: '.$title.'">Edit</a> ';
             //  $html .= ' |  <a href="submitReport.php?busId='.$id.'" title="Submit Business: '.$title.' for review">Submit for review</a> ';
               //$html .= ' | <a href="deleteReport.php?busId='.$id.'" title="delete Business: '.$title.'">Delete</a>';
               $html .= '</span></span><br/><br/>';
            $ct++;
            }
            
            echo $html;
            }
}
       
 
function showScenarioLinks(){
   
            $scenarios = $this->getScenarios();
$html="";            
            if (sizeof($scenarios)>0){ 
           $ct=1;     
           
           //var_dump($scenarios);
           
            foreach ($scenarios as $scenario){ 
                 $id=$title=$thumb = "";  
                 
                    
                    
                    $id = $scenario[0];
                    $title = $scenario[1];
                    $desc = $scenario[2];
                    
                   // sprintf('<a href="?Scenario=%s" title="%s"> %s</a> <br />', $scenario[0],  $scenario[1],  $scenario[1]);
                   
                
               $html .= $ct .':<span class="links"> <a href="showScenario.php?scnId='.$id.'" title="view Scenario: '.$title.'">'.$title.'</a> ';
               $html .= ' ::  <span class="actionlinks">';
               $html .= ' <a href="showScenario.php?scnId='.$id.'" title="view Scenario: '.$title.'">Preview</a> ';
               $html .= ' |  <a href="showScenario.php?scnId='.$id.'&scnAct=editScn" title="edit Scenario: '.$title.'">Edit</a> ';
             //  $html .= ' |  <a href="submitReport.php?scnId='.$id.'" title="Submit Scenario: '.$title.' for review">Submit for review</a> ';
               //$html .= ' | <a href="deleteReport.php?scnId='.$id.'" title="delete Scenario: '.$title.'">Delete</a>';
               $html .= '</span></span><br/><br/>';
            $ct++;
            }
            
            echo $html;
            }
}
       
 

function showBusiness($id=0, $act){
    
if (intval($id)>0){  
                                                     
      
     $businessObj = $this->getBusinesses($id); 

$html =$topHtml=$leftHtml =$rightHtml = "";    

            
            
            
 if (sizeof($businessObj)>0){ 
     
$cct=1;     
  $sId = $sTitle = $sDesc= "";  
  /*
  echo "----------------<br/>";
    var_dump($reportObj);
    echo "----------------<br/>";       
    */
    $sId = $businessObj[0][0];
    $sTitle = $businessObj[0][1];
    $sDesc = $businessObj[0][2];
    $bScenarios = (!empty($businessObj[0][3]))? explode(',', $businessObj[0][3]): '';
    
    
        
$leftHtml .='<div class="repComments brd"><h3> Business Scenario/s:</h3><br />';    
  
  if (sizeof($bScenarios)>0){ 
    $leftHtml .= '<div class="lpd100">';
            foreach ($bScenarios as $bScenario){ 
               
                $scenarioObj = $this->getScenarios($bScenario);
                
                $sId = $scenarioObj[0][0];
                $sTitle = $scenarioObj[0][1];
                    
                   
               $leftHtml.="<div class=\"cItem\">";
               
               $leftHtml .= $cct .':'.'<a href="showScenario.php?scnId='.$sId.'">'.$sTitle.'</a>';

               $leftHtml .= '</div><br />';
                
              
            $cct++;
            }
$leftHtml .= '</div>';            
  }else{
               
               $leftHtml .= " no Comments available for the Step yet<br /><br />".
               '<a href="editComment.php?cAct=addNew&repId='.$repId.'" title="add for Step '.$repId.'">Add Comment</a>'.'<br />';
  }
  
  
$leftHtml .= '<div class="cItem"><br />';           
$leftHtml .= '<a href="showBusiness.php?busId='.$sId.'&busAct=editBus" title="edit Business: '.$sTitle.'">Edit Business</a>';           

$leftHtml .= '</div>';           
$leftHtml .="</div>"; 



$topHtml ='<div class="repTitle brd">Business Title: '.$sTitle.'</div>';                  
$topHtml .='<div class="repTitle brd">Business Description: '.$sDesc.'</div>';                  
//$rightHtml = '<div class="repImage brd"><div class="repImg"><img src="'.EDF_THUMBNAILS_FOLDER.$repImage.'" alt="" class="repImg" /></div>';
//$rightHtml = '<div class="repImage brd"><img src="'.EDF_THUMBNAILS_FOLDER.$repImage.'" alt="" class="repImg" />';
//$rightHtml .= '<div class="cItem"><br /><br /><a href="showScenario.php?scnId='.$id.'&scnAct=editScn" title="edit report: '.$sTitle.'">Edit Scenario</a> </div></div>';

            
               

$html = $topHtml . $leftHtml . $rightHtml;  
 
 echo   $html;         
            }   




}

   
           
            

} 

function showScenario($id=0, $act){
    
if (intval($id)>0){  
     
     $comments = $this->getCommentsForReport($id); 
      
     $scenarioObj = $this->getScenarios($id); 

$html =$topHtml=$leftHtml =$rightHtml = "";    

            
            
            
 if (sizeof($scenarioObj)>0){ 
     
$cct=1;     
  $sId = $sTitle = $sDesc= "";  
  /*
  echo "----------------<br/>";
    var_dump($reportObj);
    echo "----------------<br/>";       
    */
    $sId = $scenarioObj[0][0];
    $sTitle = $scenarioObj[0][1];
    $sDesc = $scenarioObj[0][2];
    $sReports = (!empty($scenarioObj[0][3]))? explode(',', $scenarioObj[0][3]): '';
    
    
        
$leftHtml .='<div class="repComments brd"><h3> Scenario Steps:</h3><br />';    
  
  if (sizeof($sReports)>0){ 
    $leftHtml .= '<div class="lpd100">';
            foreach ($sReports as $sReport){ 
               
                $reportObj = $this->getReports($sReport);
                
                $rId = $reportObj[0][0];
                $rTitle = $reportObj[0][1];
                    
                   
               $leftHtml.="<div class=\"cItem\">";
               
               $leftHtml .= $cct .':'.'<a href="showReport.php?repId='.$rId.'">'.$rTitle.'</a>';

               $leftHtml .= '</div><br />';
                
              
            $cct++;
            }
$leftHtml .= '</div>';            
  }else{
               
               $leftHtml .= " no Comments available for the Step yet<br /><br />".
               '<a href="editComment.php?cAct=addNew&repId='.$repId.'" title="add for Step '.$repId.'">Add Comment</a>'.'<br />';
  }
  
  
$leftHtml .= '<div class="cItem"><br />';           
$leftHtml .= '<a href="showScenario.php?scnId='.$sId.'&scnAct=editScn" title="edit Step: '.$sTitle.'">Edit Scenario</a>';           
$leftHtml .= '<br/><a href="playScenario.php?scnId='.$sId.'&scnAct=playScn" title="play scenario Steps: '.$sTitle.'">Play Scenario</a>';           
$leftHtml .= '</div>';           
$leftHtml .="</div>"; 



$topHtml ='<div class="repTitle brd">Scenario Title: '.$sTitle.'</div>';                  
$topHtml .='<div class="repTitle brd">Scenario Description: '.$sDesc.'</div>';                  
//$rightHtml = '<div class="repImage brd"><div class="repImg"><img src="'.EDF_THUMBNAILS_FOLDER.$repImage.'" alt="" class="repImg" /></div>';
//$rightHtml = '<div class="repImage brd"><img src="'.EDF_THUMBNAILS_FOLDER.$repImage.'" alt="" class="repImg" />';
//$rightHtml .= '<div class="cItem"><br /><br /><a href="showScenario.php?scnId='.$id.'&scnAct=editScn" title="edit report: '.$sTitle.'">Edit Scenario</a> </div></div>';

            
               

$html = $topHtml . $leftHtml . $rightHtml;  
 
 echo   $html;         
            }   




}

   
           
            

}


function showFullReport($id=0, $act){
if (intval($id)>0){  
     
     $comments = $this->getCommentsForReport($id); 
     $reportObj = $this->getReports($id); 

$html =$topHtml=$leftHtml =$rightHtml = "";    

            
            
            
 if (sizeof($reportObj)>0){ 
           $cct=1;     
  $cId = $cTitle = $repTitle = $repId = $repImage = $theComment = "";  
  /*
  echo "----------------<br/>";
    var_dump($reportObj);
    echo "----------------<br/>";
    
    */
    $repId = $reportObj[0][0];
    $repTitle = $reportObj[0][1];
    $repImage = $reportObj[0][2];
    $repUrl = $reportObj[0][4];
    
        
$leftHtml .='<div class="repComments brd">';    
  
  if (sizeof($comments)>0){  
            foreach ($comments as $comment){ 
               
                   
                foreach ($comment as $key=>$val){ 
                    
                    
                    if ($key==0){ $cId = $val;}
                    if ($key==1){ $cTitle = $val;}
                    if ($key==2){ $theComment = $val;}

                    
                    
                   // sprintf('<a href="?report=%s" title="%s"> %s</a> <br />', $report[0],  $report[1],  $report[1]);
                   
                }
                
               $leftHtml.="<div class=\"cItem\">";
               //$leftHtml .= $cct .':Report  Title: '.$repTitle.'<br/>';
               $leftHtml .= $cct .':';
               $leftHtml .='Comment  Title: '.$cTitle.'<br />Comment: '.BBCode($theComment).'<br />';
               if ($act=='edit'){
               $leftHtml .= '<a href="editComment.php?cId='.$cId.'&cAct=edit" title="edit this comment '.$cId.'">Edit Comment</a>'.'<br />';
               }
                $leftHtml .= '</div><br /><br />';
                
              
            $cct++;
            }
  }else{
               
               $leftHtml .= " no Comments available for the Step yet<br /><br />".
               '<a href="editComment.php?cAct=addNew&repId='.$repId.'" title="add for Step '.$repId.'">Add Comment</a>'.'<br />';
  }
           
$leftHtml .="</div>"; 



$topHtml ='<div class="repTitle brd">Step  Title: '.$repTitle.'</div>';                  
//$rightHtml = '<div class="repImage brd"><div class="repImg"><img src="'.EDF_THUMBNAILS_FOLDER.$repImage.'" alt="" class="repImg" /></div>';

$rightHtml = '<div class="repImage cItem"><img src="'.EDF_THUMBNAILS_FOLDER.$repImage.'" alt="" class="repImg" /><br/>';



if (!empty($repUrl)){
$rightHtml .= '<div class="floatLeft"><a target="_blank" href="'.rawurldecode($repUrl).'" class="" title="">View Step Url</a> <br/></div>';
//$rightHtml .= '<div class="clear floatLeft"><br/><iframe src="'.rawurldecode($repUrl).'" class="" id="urlIframe" /></div>';
}


                                      
if ($act=='edit'){
    $rightHtml .= '<div class="clear floatLeft"><br/><br/><a href="showReport.php?repId='.$id.'&repAct=editReport" title="edit Step: '.$repTitle.'">Edit Step</a></div>';
}

    
$rightHtml .= '</div>';

            
               

$html = $topHtml . $leftHtml . $rightHtml;  
 
 echo   $html;         
            }   




}

   
           
            

}



function showForm($type, $repId, $cId, $action){ 
 
 if (!empty($type) && $repId>0){   
  
    switch ($type){
        
        case 'comment':
        
        if ($action =='add'){
            
            $formTitle =  'Add a new Comment to this Step';
            
        }   else{
            
            $formTitle =  'Edit Comment on Step: '.$repId;
            
            
            
        }
        
        break;
        case 'report':
        
        if ($action =='add'){
            
            $formTitle =  'Add a new Step';
            
        }   else{
            
            $formTitle =  'Edit Step: '.$repId;
            
            
        }
        
        break;
        
    }
    
 }
    
    
}


function addVote($wbm, $user, $vote){
    
            if (!empty($wbm)){
                
            $vote_date =date('Y-m-d H:i:s');    
            
                if (!$this->userVoted($wbm, $user)){
                    
                    $sqlStr = "INSERT INTO wbm_votes 
                    (wbm_id, wbm_fb_user, wbm_vote, vote_date) 
                    VALUES(
                    $wbm,
                    $user,
                    $vote,
                    '$vote_date'
                    )";
        
        
                    $sucess = Database::runInsertQuery($sqlStr);
                       if ($sucess!="Database error"){
                        echo "Your vote has been registered.<br/>Thanks" ;
                       }
        
               
                }else{
                 
                 // do nothing only 1 vote allowed
                 /*
                    $sqlStr = "UPDATE wbm_votes  set 
                    wbm_vote = $vote , vote_date = '$vote_date' 
                    WHERE 
                    wbm_fb_user = $user AND  wbm_id=$wbm 
                    ";
                     //echo   $sqlStr;
                    $sucess = Database::runUpdateQuery($sqlStr);
                    if ($sucess!="Database error"){
                    echo "Your vote has been Updated.<br/>Thanks" ;
                    }
                    */
                }
            }   
             
            
            
           

            }




function userVoted($wbm, $user){
 
$retval = false;
if (!$wbm && !$user) return $retval;

            $sqlStr = "Select count(id) from wbm_votes 
            WHERE wbm_id=$wbm AND wbm_fb_user = $user ";           
            
            //$sqlStr = "Select count(id) from votes WHERE  wbm_fb_user = $user ";
            
            $result = Database::runSelectQuery($sqlStr);
            
            //print_r($result);
            //exit;
            $count=$result[0][0];

            
           if ($result!="Database error" && $count>0){
            $retval = true;     
           }
            
return $retval ;   

}

function hasBlondeMoments($user){
 $retval = false;
if (!$user) return $retval;

            /*$sqlStr = "Select count(id) from votes 
            WHERE wbm_id=$wbm AND wbm_fb_user = $user ";           
            */
            $sqlStr = "Select id from wbm_blonde_moments WHERE  wbm_author = $user AND wbm_active=1 limit 0,1";
            
            $result = Database::runSelectQuery($sqlStr);
            
         //  print_r($result);
           $count = 0;
            //exit;
            if (sizeof($result)>0){
            $count=$result[0][0];    
            }
            
            

            
           if ($result!="Database error" && $count>0){
            $retval = $count;     
           }
            
return $retval ;   

}
 



function showVotes($wbm_id){
$ratingHTML = ""; 
 
if (!$wbm_id) return $ratingHTML;

            $sqlStr = "Select count(id) as votes_count, SUM(wbm_vote) AS total_votes_value  from wbm_votes 
            WHERE wbm_id=$wbm_id ";
            

            $result = Database::runSelectQuery($sqlStr);
            $votes_count=$result[0][0];        
           if ($result!="Database error" && $votes_count>0  ){
            //print_r($result) ;
            $votes_count=$result[0][0];
            $total_votes_value=$result[0][1];
            
            $avg = round($total_votes_value/$votes_count*100)/100;
            $perc = round( (100/5)*$avg);
            $rem = 100-$perc;
                            
                $ratingHTML= '  
               <div style="clear: both;"/>  
                <table id="dums" align=left width=150 height=15 bgcolor="#009900" cellpadding=0 cellspacing=0 style="font-size: 13px; border: #449944 solid 2px;">
                <tr width=150>
                <td bgcolor="red" width='.$perc.'
                </td>
                <td bgcolor="#F89696"  width='.$rem.'
                </td>
                <td bgcolor="#ffffff" align=center width=50 style="font-size: 12px; border: #449944 solid 4px; border-bottom-width: 1px; border-top-width: 1px; border-right-width: 1px;">
                '.$perc."%".'
                </td>
                </tr>
                </table> 

                ';

    
           }
            
echo $ratingHTML;  
 
}

}
//echo "wella lib";
?>
